package com.blog.config;

import com.blog.interceptor.JwtTokenAdminInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;


/**
 * 配置类，注册web层相关组件
 */
@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    @Autowired
    private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;

    /**
     * 注册自定义拦截器用于校验JWT(前端通过接口传过来的请求)
     */
    protected void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");
        //注册JWT拦截器
        registry.addInterceptor(jwtTokenAdminInterceptor)
                //拦截规则（/admin/下的所有请求）
                .addPathPatterns("/admin/**")
                //不拦截（/admin/user/login请求）
                .excludePathPatterns("/admin/user/login")
                .excludePathPatterns("/doc.html")
                .excludePathPatterns("/user/**");
    }

    /**
     * 设置静态资源映射（方向浏览器的静态资源访问）
     */
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        //Edge
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        //Chrome
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}
